package com.shopping.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopping.commons.PageParams;
import com.shopping.mapper.BookMapper;
import com.shopping.mapper.CommentMapper;
import com.shopping.model.Book;
import com.shopping.model.view.CommentView;
import com.shopping.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;
    private CommentMapper commentMapper;

    private RedisTemplate redisTemplate;

    //构造器注入，并设置序列化Redis
    @Autowired
    public BookServiceImpl(BookMapper bookMapper, CommentMapper commentMapper, RedisTemplate redisTemplate) {
        this.bookMapper = bookMapper;
        this.commentMapper = commentMapper;
        this.redisTemplate = redisTemplate;
        this.redisTemplate.setKeySerializer(new StringRedisSerializer());
        this.redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

    }

    public BookServiceImpl() {
    }

    @Override
    public Map<String, Object> queryCommentById(Integer book_id, PageParams pageParams) {
        //使用PageHelper实现分页
        //开始分页插件
        PageHelper.startPage(pageParams);
        List<CommentView> commentViews = commentMapper.queryCommentById(book_id);
        //将查询结果封装到PageInfo对象中，该对象是PageHelper提供的
        //该对象会根据commentViews来封装分页相关数据
        PageInfo<CommentView> pageInfo = new PageInfo<>(commentViews);
        //将分页需要返回的客户端数据封装到Map结合中
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("commentList", pageInfo.getList());
//        System.out.println(map.get("total")+"///////"+map.get("commentList"));
        return map;
    }

    @Override
    public List<Book> queryBook(Integer... isDisplay) {
        //从数据库获取数据
        List<Book> bookList = bookMapper.queryBook(isDisplay);
        Map<String, Object> booksMap = new HashMap<>();
        //检测商品是否上架
        if (isDisplay[0] == 0) {
            //获得hash操作对象
            HashOperations hashOperations = redisTemplate.opsForHash();
            Map bookListMap = hashOperations.entries("bookList");
            //检测Redis中是否存在数据,如不存在从数据库中获取数据
            if (bookListMap.size() == 0) {
                //遍历bookList，将bookList中数据转存到booksMap集合中
                for (Book book : bookList) {
                    booksMap.put("BOOKKEY:" + book.getBook_id(), book);
                }
                //将booksMap集合存到Redis中，RedisKey为BookList
                hashOperations.putAll("bookList", booksMap);
                return bookList;
            } else {//否则从Redis中直接获取数据
                    Collection<Book> values = bookListMap.values();
                    List<Book> bookLists = new ArrayList<>();
                    for (Book book : values) {
                        bookLists.add(book);
                    }
                    return bookLists;
            }
        } else {
            return bookList;
        }
    }

    @Override
    public Book queryBookById(Integer book_id) {
        return bookMapper.queryBookById(book_id).get(0);
    }
}
